package hk.dao.impl;

import hk.dao.UserDao;
import hk.pojo.UserInfo;
import hk.util.DBUtil;

import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/4/2 21:44
 */
public class UserDaoImpl implements UserDao {
    @Override
    public UserInfo getUserById(int id) {
        return DBUtil.getSingleObj("select * from userinfo where id = ?",UserInfo.class,id);
    }

    @Override
    public UserInfo login(String userName, String password) {
        return DBUtil.getSingleObj("select * from userinfo where userName = ? and password = ?",UserInfo.class,userName,password);
    }

    @Override
    public List<UserInfo> getAll() {
        return DBUtil.getList("select * from userInfo" ,UserInfo.class);
    }

    @Override
    public String getUserNameById(int id) {
        return DBUtil.getScala("select userName from userinfo where id = ?",id);
    }

    @Override
    public int getUserCount() {
        long count = DBUtil.getScala("select count(*) from userinfo");
        return Integer.parseInt(count+"");
    }

    @Override
    public int addUser(UserInfo u) {
        return DBUtil.update("insert into userinfo(userName,password) values (?,?)",u.getUserName(),u.getPassword());
    }

    @Override
    public int updateUser(UserInfo u) {
        return DBUtil.update("update userinfo set userName = ?,password = ? where id = ?",u.getUserName(),u.getPassword(),u.getId());
    }

    @Override
    public int deleteUserById(int id) {
        return DBUtil.update("delete from userinfo where id = ?",id);
    }

    @Override
    public int addAndReturnId(UserInfo u) {
        return DBUtil.addAndReturnId("insert into userinfo (username,password) values (?,?)",u.getUserName(),u.getPassword());
    }

    @Override
    public List<String> getUserNames() {
        return DBUtil.getColumn("select username from userinfo");
    }
}
